<?php

namespace App\Console\Gather;


use App\Models\SiteStock;
use QL\QueryList;

class DarkHorse extends Base
{

    // 创业黑马
    /**
     * 请求：
     *  php artisan command:gather-dark-horse
     *  sudo -u www php /www/wwwroot/slh/laravel.test/artisan command:gather-dark-horse
     *
     */
    /**
     * 参数与选项的区别
     *      在执行命令时，参数必须严格按定义时的顺序输入，不需输入参数名，只需输入参数的值即可。
     *      输入选项则必须带选项名，且选项名前须加 -- 前缀（选项简写字母前加 -），选项可按任意顺序输入
     */

    // 请求 以及参数
    // protected $signature = 'command:name {param1} {param2}';  // 必填参数
    // protected $signature = 'command:name {参数名?} {参数名=默认值} {参数名? : 参数说明}';  // 可选参数
    // protected $signature = 'command:name {--选项名} {--选项名=} {--选项名=默认值} {--简写字母|选项名} {--选项名 : 选项说明}';  // 选项
    protected $signature = 'command:gather-dark-horse';

    protected $description = '黑马创业：从新浪财经，新浪财经：研报，股吧网站采集黑马创业的资讯';
    protected $ctsDb;
    protected $toDb;


    // 执行的命令
    public function handle()
    {
        $weIds = [61];
        $time  = time();
        $uid   = 10199; // 黑马的用户

        // 获取所有的股票信息
        $stockDb = new SiteStock();
        $stocks  = $stockDb->get()->toArray();

        // 从新浪财经网站采集数据
        $this->sina($uid, $time, $weIds, $stocks);

        // 从新浪财经：研报网站采集数据
        $this->stock($uid, $time, $weIds, $stocks);

        // 从股吧网站采集数据
        $this->guba($uid, $time, $weIds, $stocks);
    }

    // 从新浪财经网站采集数据：手机端（采集和pc端的一样 因为采集页面会自动转跳到pc端）
    protected function edit_sinaMobile($uid, $time, $weIds, $stocks = [])
    {
        // 获取列表
        $url = 'https://stocknews.cj.sina.cn/stocknews/api/news/get?market=cn&symbol=sz300688&su2cu=1&page=1&num=30&fr=wap&mi=1&_=1697620780834&callback=jsonp2';
        // TODO 修改采集 获取文章
        //$res   = $this->curl()->request($url); // 采集返回的是 try 需要整理成json
        $start = strpos($res, '(');
        $end   = strpos($res, ');');
        $json  = substr($res, $start + 1, $end - $start - 1);
        $rst   = json_decode($json, true);
        $lists = $rst['result']['data'] ?? [];
        $today = strtotime(date('Y-m-d'));

        // 采集详情
        // 采集和pc端的一样 因为页面会自动转跳到pc端
        foreach($lists as $key => $item){
            $_title = $item['title'];
            $_ctime = $item['ctime'];  // 时间
            $wapurl = $item['wapurl'];
            $_href  = $item['url'];
            // 判断链接是否已经采集
            $info = $this->isGather($_href);
            if($info){
                $this->showMsg(' '.($key + 1).'：==> 已经采集文章：'.$_title);
                continue;
            }

            // 只采集今天的
            if($_ctime < $today){
                $this->showMsg(' '.($key + 1).'：==> 文章：不是今天的文章：'.$_title);
                continue;
            }

            // 如果没有标题
            if(empty($_title)){
                continue;
            }
            // 查询
            $this->showMsg(' '.($key + 1).'：==> 采集文章：'.$_title);
            $_ql = QueryList::get($_href);
            // 如果有 p 标签
            $rule  = ['html' => ['', 'html'], 'text' => ['', 'text']];
            $list  = $_ql->range('#artibody p')->rules($rule)->queryData();
            $_text = '';
            $_desc = '';
            if($list){
                foreach($list as $_item){
                    // 这里不直接用 $_item['text']  避免里面有图片
                    $_rangeHtml = $this->removeHtml($_item['html']);
                    if(empty($_rangeHtml)){
                        continue;
                    }
                    if(empty($_desc) && (strpos($_rangeHtml, '记者') === false) && (strpos($_rangeHtml, '转自') === false) && (strpos($_rangeHtml, '|') === false) && (strpos($_rangeHtml, '来源') === false)){
                        $_desc = $_rangeHtml;
                    }
                    $_text .= '<p style="padding: 3px 0;">'.$_rangeHtml.'</p>';
                }
            } else{
                // 如果没有 p 标签
                // 这里不直接用 ->text()  避免里面有图片
                //$_desc = $_ql->find('#artibody')->text();
                $_html = $_ql->find('#artibody')->html();
                $_html = $this->removeHtml($_html);
                $_desc = $_html;
                if($_html){
                    $_text .= '<p style="padding: 3px 0;">'.$_html.'</p>';
                }
            }

            // 对比股票信息
            $_has = [];  // 用股票的id
            foreach($stocks as $sit){
                $_name = $sit['name'];
                if(strpos($_text, $_name) !== false){
                    $_has[] = $sit['id'];
                }
            }

            // echo chr(10).chr(10).chr(10);
            // var_dump($_title);
            // var_dump($_href);
            // var_dump($_ctime);
            // var_dump($today);
            // var_dump($_desc);
            //var_dump($_text);
            //var_dump($_html);
            $this->showMsg('      ==> 保存到数据库：'.$_title);
            $this->saveArticle($uid, $_href, $_title, $_desc, $_text, $time, $_author = '', $is_show = false, $_has);
            $this->showMsg('      ==> 发送到微信：'.var_export($weIds, 1));
            $this->wechat($_title, $_href, $weIds);
        }

        return true;
    }

    // 从新浪财经网站采集数据
    protected function sina($uid, $time, $weIds, $stocks = [])
    {
        // 获取列表
        $url   = 'https://vip.stock.finance.sina.com.cn/corp/go.php/vCB_AllNewsStock/symbol/sz300688.phtml';
        $rule  = ['href' => ['', 'href'], 'title' => ['', 'text']];
        $lists = $this->getList($url, '#con02-7 table td .datelist', 'ul a', $rule);
        $today = strtotime(date('Y-m-d'));
        $today = strtotime(date('Y-m-d')) - 86400 * 2;

        // 采集详情
        foreach($lists as $key => $item){
            $_title  = $item['title'];
            $_href   = $item['href'];
            $_author = '';
            // 判断链接是否已经采集
            $info = $this->isGather($_href);
            if($info){
                $this->showMsg(' '.($key + 1).'：==> 已经采集文章：'.$_title);
                continue;
            }
            // 查询
            $_ql = QueryList::get($_href);
            // 时间
            $_date1 = $_ql->find('.date')->text();
            $_date2 = $_ql->find('.news-date')->text();
            $_date  = $_date1 ?: $_date2;
            if(strpos($_date, '年') === false){
                $_date = date("Y年").$_date;
            }
            $_date  = str_replace('年', '-', $_date);
            $_date  = str_replace('月', '-', $_date);
            $_date  = str_replace('日', ' ', $_date);
            $_ctime = strtotime($_date);

            // 只采集今天的
            if($_ctime < $today){
                $this->showMsg(' '.($key + 1).'：==> 文章：不是今天的文章：'.$_title);
                continue;
            }

            // 如果没有标题
            if(empty($_title)){
                continue;
            }
            // 如果有 p 标签
            $this->showMsg(' '.($key + 1).'：==> 采集文章：'.$_title);
            $rule  = ['html' => ['', 'html'], 'text' => ['', 'text']];
            $list  = $_ql->range('#artibody p')->rules($rule)->queryData();
            $_text = '';
            $_desc = '';
            if($list){
                foreach($list as $_item){
                    // 这里不直接用 $_item['text']  避免里面有图片
                    $_rangeHtml = $this->removeHtml($_item['html']);
                    if(empty($_rangeHtml)){
                        continue;
                    }
                    if(empty($_desc) && (strpos($_rangeHtml, '记者') === false) && (strpos($_rangeHtml, '转自') === false) && (strpos($_rangeHtml, '|') === false) && (strpos($_rangeHtml, '来源') === false)){
                        $_desc = $_rangeHtml;
                    }
                    $_desc = str_replace('创业黑马(300688.SZ)', '', $_desc);
                    $_text .= '<p style="padding: 3px 0;">'.$_rangeHtml.'</p>';
                }
            } else{
                // 如果没有 p 标签
                // 这里不直接用 ->text()  避免里面有图片
                //$_desc = $_ql->find('#artibody')->text();
                $_html = $_ql->find('#artibody')->html();
                $_html = $this->removeHtml($_html);
                $_desc = str_replace('创业黑马(300688.SZ)', '', $_html);
                if($_html){
                    $_text .= '<p style="padding: 3px 0;">'.$_html.'</p>';
                }
            }

            // 对比股票信息
            $_has = [];  // 用股票的id
            foreach($stocks as $sit){
                $_name = $sit['name'];
                if(strpos($_text, $_name) !== false){
                    $_has[] = $sit['id'];
                }
            }


            // 组合参数用于调用
            // echo chr(10).chr(10).chr(10);
            // var_dump($_date);
            // var_dump($today);
            // var_dump($_html);
            // $_ctime  $time
            $_row = ['uid' => $uid, 'link' => $_href, 'title' => $_title, 'description' => $_desc, 'text' => $_text, 'time' => $time, 'author' => $_author, 'is_show' => true, 'to_channel' => 'dark', 'from_site' => 'sina', 'has' => $_has];
            $this->showMsg('      ==> 保存到数据库：'.$_title);
            $this->saveArticle($_row);
            $this->showMsg('      ==> 发送到微信：'.var_export($weIds, 1));
            $this->wechat($_title, $_href, $weIds);
        }

        return true;
    }

    // 从新浪财经：研报网站采集数据
    protected function stock($uid, $time, $weIds, $stocks = [])
    {
        // 获取列表
        $url   = 'https://stock.finance.sina.com.cn/stock/go.php/vReport_List/kind/search/index.phtml?symbol=300688&t1=all';
        $url   = 'https://stock.finance.sina.com.cn/stock/go.php/vReport_List/kind/search/index.phtml?symbol=300688&t1=all';
        $rule  = ['html' => ['', 'html']];
        $lists = $this->getList($url, '.wrap ', '.main table tr', $rule);

        $today = strtotime(date('Y-m-d'));
        $today = strtotime(date('Y-m-d')) - 86400 * 2;
        $_rule = ['html' => ['', 'html'], 'text' => ['', 'text'], 'link' => ['a', 'href']];
        foreach($lists as $key => $item){
            $_ql   = QueryList::html($item['html']);
            $_list = $_ql->range('td')->rules($_rule)->queryData();
            if(count($_list) < 6){
                continue;
            }
            // 组合数据
            $_item = [
                'index'  => $_list[0]['text'],
                'title'  => trim($_list[1]['text']),
                'href'   => trim($_list[1]['link']),
                'type'   => $_list[2]['text'],
                'date'   => $_list[3]['text'],
                'origin' => trim($_list[4]['text']),
                'author' => $_list[5]['text'],
            ];

            // 执行采集
            $_title  = $_item['title'];
            $_href   = $_item['href'];
            $_date   = $_item['date'];
            $_ctime  = strtotime($_date); // 时间
            $_author = '';
            //采集链接
            if(strpos($_href, 'http') === false){
                $_href = 'https:'.$_href;
            }

            // 判断链接是否已经采集
            $info = $this->isGather($_href);
            if($info){
                $this->showMsg(' '.($key + 1).'：==> 已经采集文章：'.$_title);
                continue;
            }
            // 只采集今天的
            if($_ctime < $today){
                $this->showMsg(' '.($key + 1).'：==> 文章：不是今天的文章：'.$_title);
                continue;
            }

            // 如果没有标题
            if(empty($_title)){
                continue;
            }

            // 查询
            $_ql = QueryList::get($_href);
            // 如果有 p 标签
            $this->showMsg(' '.($key + 1).'：==> 采集文章：'.$_title);
            $rule  = ['html' => ['', 'html'], 'text' => ['', 'text']];
            $list  = $_ql->range('.content .blk_container p')->rules($rule)->queryData();
            $_text = '';
            $_desc = '';
            if($list){
                foreach($list as $_item){
                    // 这里不直接用 $_item['text']  避免里面有图片
                    $_rangeHtml = $this->removeHtml($_item['html']);
                    if(empty($_rangeHtml)){
                        continue;
                    }
                    if(empty($_desc) && (strpos($_rangeHtml, '记者') === false) && (strpos($_rangeHtml, '转自') === false) && (strpos($_rangeHtml, '|') === false) && (strpos($_rangeHtml, '来源') === false)){
                        $_desc = $_rangeHtml;
                    }
                    $_desc = str_replace('创业黑马(300688.SZ)', '', $_desc);
                    $_text .= '<p style="padding: 3px 0;">'.$_rangeHtml.'</p>';
                }
            } else{
                // 如果没有 p 标签
                // 这里不直接用 ->text()  避免里面有图片
                //$_desc = $_ql->find('#artibody')->text();
                $_html = $_ql->find('.content .blk_container')->html();
                $_html = $this->removeHtml($_html);
                $_desc = str_replace('创业黑马(300688.SZ)', '', $_html);
                if($_html){
                    $_text .= '<p style="padding: 3px 0;">'.$_html.'</p>';
                }
            }

            // 对比股票信息
            $_has = [];  // 用股票的id
            foreach($stocks as $sit){
                $_name = $sit['name'];
                if(strpos($_text, $_name) !== false){
                    $_has[] = $sit['id'];
                }
            }

            // echo chr(10).chr(10).chr(10);
            // var_dump($_date);
            // var_dump($today);
            //var_dump($_html);
            $_row = ['uid' => $uid, 'link' => $_href, 'title' => $_title, 'description' => $_desc, 'text' => $_text, 'time' => $time, 'author' => $_author, 'is_show' => true, 'to_channel' => 'dark', 'from_site' => 'sina_stock', 'has' => $_has];
            $this->showMsg('      ==> 保存到数据库：'.$_title);
            $this->saveArticle($_row);
            $this->showMsg('      ==> 发送到微信：'.var_export($weIds, 1));
            $this->wechat($_title, $_href, $weIds);
        }

        return true;
    }

    // 从股吧网站采集数据
    protected function guba($uid, $time, $weIds, $stocks = [])
    {
        // 获取列表
        $url   = 'https://guba.eastmoney.com/list,300688.html';
        $rule  = ['html' => ['', 'html'], 'title' => ['', 'text']];
        $lists = $this->getList($url, '#mainlist .table_list .tab_content .defaultlist', 'table tr', $rule);

        $domain = 'https://guba.eastmoney.com';
        $today  = strtotime(date('Y-m-d'));
        $year   = date('Y');
        $_rule  = ['html' => ['', 'html'], 'text' => ['', 'text'], 'link' => ['a', 'href']];
        foreach($lists as $key => $item){
            $_ql   = QueryList::html($item['html']);
            $_list = $_ql->range('td')->rules($_rule)->queryData();
            if(count($_list) < 5){
                continue;
            }
            // 组合数据
            $_item = [
                'read_no' => $_list[0]['text'],
                'comment' => trim($_list[1]['text']),
                'title'   => trim($_list[2]['text']),
                'href'    => $_list[2]['link'],
                'author'  => trim($_list[3]['text']),
                'date'    => $year.'-'.$_list[4]['text'],
            ];

            // 执行采集
            $_title  = $_item['title'];
            $_href   = $_item['href'];
            $_date   = $_item['date'];
            $_author = $_item['author'];
            $_ctime  = strtotime($_date); // 时间
            // 只采集有评论的
            /*if($_item['comment'] < 1){
                $this->showMsg(' '.($key + 1).'：==> 文章：评论数过少，不采集：'.$_title);
                continue;
            }*/
            // 采集地址
            //采集链接
            if(strpos($_href, 'http') === false){
                if(strpos($_href, '//') === false){
                    $_href = $domain.$_href;
                } else{
                    $_href = 'https:'.$_href;
                }
            }

            // 判断链接是否已经采集
            $info = $this->isGather($_href);
            if($info){
                $this->showMsg(' '.($key + 1).'：==> 已经采集文章：'.$_title);
                continue;
            }
            // 只采集今天的
            if($_ctime < $today){
                $this->showMsg(' '.($key + 1).'：==> 文章：不是今天的文章：'.$_title);
                continue;
            }

            // 如果没有标题
            if(empty($_title)){
                continue;
            }
            // 查询
            $_ql = QueryList::get($_href);
            // 如果有 p 标签
            $this->showMsg(' '.($key + 1).'：==> 采集文章：'.$_title);
            $rule  = ['html' => ['', 'html'], 'text' => ['', 'text']];
            $list  = $_ql->range('#newscontent .newstext  p')->rules($rule)->queryData();
            $_text = '';
            $_desc = '';
            if($list){
                foreach($list as $_item){
                    // 这里不直接用 $_item['text']  避免里面有图片
                    $_rangeHtml = $this->removeHtml($_item['html']);
                    if(empty($_rangeHtml)){
                        continue;
                    }
                    if(empty($_desc) && (strpos($_rangeHtml, '记者') === false) && (strpos($_rangeHtml, '转自') === false) && (strpos($_rangeHtml, '|') === false) && (strpos($_rangeHtml, '来源') === false)){
                        $_desc = $_rangeHtml;
                    }
                    $_desc = str_replace('创业黑马(300688.SZ)', '', $_desc);
                    $_text .= '<p style="padding: 3px 0;">'.$_rangeHtml.'</p>';
                }
            } else{
                // 如果没有 p 标签
                // 这里不直接用 ->text()  避免里面有图片
                //$_desc = $_ql->find('#artibody')->text();
                $_html = $_ql->find('#newscontent .newstext ')->html();
                $_html = $this->removeHtml($_html);
                $_desc = str_replace('创业黑马(300688.SZ)', '', $_html);
                if($_html){
                    $_text .= '<p style="padding: 3px 0;">'.$_html.'</p>';
                }
            }

            // 对比股票信息
            $_has = [];  // 用股票的id
            foreach($stocks as $sit){
                $_name = $sit['name'];
                if(strpos($_text, $_name) !== false){
                    $_has[] = $sit['id'];
                }
            }

            // echo chr(10).chr(10).chr(10);
            // var_dump($_date);
            // var_dump($today);
            //var_dump($_html);
            $_row = ['uid' => $uid, 'link' => $_href, 'title' => $_title, 'description' => $_desc, 'text' => $_text, 'time' => $time, 'author' => $_author, 'is_show' => true, 'to_channel' => 'dark', 'from_site' => $domain, 'has' => $_has];
            $this->showMsg('      ==> 保存到数据库：'.$_title);
            $this->saveArticle($_row);
            $this->showMsg('      ==> 发送到微信：'.var_export($weIds, 1));
            $this->wechat($_title, $_href, $weIds);
        }

        return true;
    }

}

